package com.edu.orderservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.user.EduCourseOrder;
import com.edu.commonutils.user.UcenterMemberOrder;
import com.edu.orderservice.client.EduCourseClient;
import com.edu.orderservice.client.UcenterMemberClient;
import com.edu.orderservice.domain.TOrder;
import com.edu.orderservice.mapper.TOrderMapper;
import com.edu.orderservice.service.TOrderService;
import com.edu.orderservice.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-26
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
     private EduCourseClient eduCourseClient;

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    @Override
    public String saveOrder(String courseId, String memberIdByJwtToken) {
        //通过远程调用获取课程详细对象
        EduCourseOrder courseInfo = this.eduCourseClient.getCourseInfoDto(courseId);

        //通过远程调用获取用户信息
        UcenterMemberOrder infoOrder = ucenterMemberClient.getInfoOrder(memberIdByJwtToken);
        //创建order对象
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(infoOrder.getMobile());
        order.setNickname(infoOrder.getNickname());
        order.setStatus(0);//订单状态 是否支付 0未支付 1已支付
        order.setPayType(1);//支付方式 1微信 ...
        //创建订单
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
